package com.example.client.support;

import com.example.client.core.ApiProperties;
import com.example.common.config.ServiceDefinition;
import com.example.common.config.ServiceInstance;
import com.example.common.util.ServiceUtil;
import com.example.register.center.api.RegisterCenter;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;


/**
 * @Author Peng Yisheng
 * @Date 2024/11/26 0:51
 * @Description 抽象客户端注册管理器，用于提供不同协议支持
 */
@Slf4j
public abstract class AbstractClientRegisterManager {
    @Getter
    private ApiProperties apiProperties;

    private RegisterCenter registerCenter;

    protected AbstractClientRegisterManager(ApiProperties apiProperties) {
        this.apiProperties = apiProperties;

        //初始化注册中心对象
        registerCenter = ServiceUtil.loadService(RegisterCenter.class);
        registerCenter.init(apiProperties.getRegisterAddress(), apiProperties.getEnv());
    }

    /**
     * 提供给子类进行注册服务实例
     *
     * @param serviceDefinition 服务定义
     * @param serviceInstance   服务实例
     */
    protected void register(ServiceDefinition serviceDefinition, ServiceInstance serviceInstance) {
        registerCenter.register(serviceDefinition, serviceInstance);
    }
}
